package cn.appsys.util;

import java.io.Serializable;

/**
 * Created by Super Cui on 2017/10/13.
 */
public class PageUtil implements Serializable {
    private Integer pageIndex;
    private Integer pageSize;
    private Integer maxCount;
    private Integer maxPage;
    private Integer beginIndex;

    /**
     * constructor
     * @param pageIndex
     * @param pageSize
     * @param maxCount
     */
    public PageUtil(Integer pageIndex, Integer pageSize, Integer maxCount) {
        setMaxCount(maxCount);
        setPageSize(pageSize);
        setMaxPage();
        setPageIndex(pageIndex);
        setBeginIndex();
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    private void setPageIndex(Integer pageIndex) {
        if (pageIndex == null || pageIndex < 1) {
            this.pageIndex = 1;
        } else if (pageIndex > maxPage) {
            this.pageIndex = maxPage;
        } else {
            this.pageIndex = pageIndex;
        }
    }

    public Integer getPageSize() {
        return pageSize;
    }

    private void setPageSize(Integer pageSize) {
        if (pageSize == null || pageSize <= 5) {
            this.pageSize = 5;
        } else {
            this.pageSize = pageSize;
        }
    }

    public Integer getMaxCount() {
        return maxCount;
    }

    private void setMaxCount(Integer maxCount) {
        if (maxCount == null || maxCount <= 5) {
            this.maxCount = 5;
        } else {
            this.maxCount = maxCount;
        }
    }

    public Integer getMaxPage() {
        return maxPage;
    }

    private void setMaxPage() {
        maxPage = (maxCount % pageSize == 0) ? (maxCount / pageSize) : (maxCount / pageSize + 1);
    }

    public Integer getBeginIndex() {
        return beginIndex;
    }

    private void setBeginIndex() {
        this.beginIndex = (pageIndex - 1) * pageSize;
    }

    @Override
    public String toString() {
        return "PageUtil{" +
                "pageIndex=" + pageIndex +
                ", pageSize=" + pageSize +
                ", maxCount=" + maxCount +
                ", maxPage=" + maxPage +
                ", beginIndex=" + beginIndex +
                '}';
    }
}
